/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mrez.zBox.gui;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import mrez.zBox.appsmodel.AppsModel;
import sun.awt.shell.ShellFolder;

/**
 *
 * @author Ahmed
 */
public final class MainWindow extends javax.swing.JFrame{
    public MainWindow( String aAppName )
    {
        super(aAppName);
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(SettingsDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        this.CreateGUI();
    }
    
    public void CreateGUI()
    {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Center the app
        //Remove title bar
        this.setUndecorated(true);
        
        
        this.add( this.DrawApps() );
        this.pack();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        this.setIconImage( SysTrayIcon.createImage("images/Box.gif", "tray icon") );
        
        
        //To make form runnable and invisible
        this.setVisible(true);
        this.setVisible(false);
    }
    
    public JPanel DrawApps()
    {
        JPanel f = new JPanel();
        //f.setSize(300, 150);
        f.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));        
        GridLayout layout = new GridLayout(0, 3, 10, 10);        
        f.setLayout(layout);
        f.setBackground(Color.DARK_GRAY);        
        
        for( int x = 0; x < AppsModel.m_AppsList.length; ++x ) {
            String title = AppsModel.m_AppsList[x].getName();
            JLabel but = new JLabel(title.substring(0, title.length() > 20 ? 20 : title.length()));
            but.putClientProperty("file", AppsModel.m_AppsList[x].getAbsolutePath());
            Icon icon = null;
            try {
                ShellFolder sf;
                sf = ShellFolder.getShellFolder(AppsModel.m_AppsList[x]);
                icon = new ImageIcon(sf.getIcon(true), sf.getFolderType());
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
            }
            but.setHorizontalTextPosition(JLabel.CENTER);
            but.setVerticalTextPosition(JLabel.BOTTOM);
            
            but.setForeground(Color.white);
            but.setIcon(icon);
            but.setHorizontalAlignment(JLabel.CENTER);
            but.setSize(new Dimension(30,30));
            but.setMaximumSize(new Dimension(30,30));
            but.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            but.addMouseListener( new MouseListener() {

                @Override
                public void mouseClicked(MouseEvent e) {
                }

                @Override
                public void mousePressed(MouseEvent e) {
                    JLabel button = (JLabel) e.getComponent();
                    try {
                        Desktop.getDesktop().open(new File((String) button.getClientProperty("file")));
                    } catch (IOException ex) {
                        Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    JLabel button = (JLabel) e.getComponent();
                    button.setBorder(BorderFactory.createLineBorder(Color.WHITE));
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    JLabel button = (JLabel) e.getComponent();
                    button.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                }

            } );
            f.add(but);
        }
        return f;
    }
}